package com.jinyuanchun.learnjava.service.impl;

import com.jinyuanchun.learnjava.service.LdapService;
import com.jinyuanchun.learnjava.service.LoginService;
import com.jinyuanchun.learnjava.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.Token;
import org.springframework.ldap.support.LdapUtils;
import org.springframework.stereotype.Service;

import javax.naming.directory.DirContext;
import java.util.HashMap;
import java.util.Map;

@Service
public class LoginServiceImpl implements LoginService {
//    @Autowired
//    private LdapTemplate ldapTemplate;
    @Autowired
    private LdapService ldapService;
    @Autowired
    private TokenService tokenService;

    @Override
    public Map<String, String> login(String usr, String pwd) {
        Map<String,String> data = new HashMap<>();
        boolean flag = ldapService.authenticate(usr,pwd);
        if (flag == true) {
            String token = tokenService.getToken(usr);
            data.put("result","ok");
            data.put("token",token);
            return data;
        } else {
            data.put("result","no");
            return data;
        }
    }
}
